import { getHouseResSearchPage } from '../../api/houseRes'
import config from '../../config/index'
Page({
  data: {
    search_key: '',
    page: 1,
    resList: [] as any[],
    config
  },
  async getList() {
    wx.showLoading({
      title: '获取中'
    })
    this.setData({
      resList: []
    })
    const res = await getHouseResSearchPage(this.data.page, this.data.search_key)
    if (res.statusCode === config.SUCCESS_CODE) {
      wx.hideLoading()
      if (res.data.data.length === 0) return
      this.setData({
        resList: res.data.data.sort((a: ListHouseRes, b: ListHouseRes) => a.house_rent - b.house_rent)
      })
    } else {
      wx.hideLoading()
      wx.showToast({ title: '无结果', icon: 'none' })
    }
  },
  async addPage() {
    wx.showLoading({
      title: '获取中'
    })
    const res = await getHouseResSearchPage(this.data.page, this.data.search_key)
    if (res.statusCode === config.SUCCESS_CODE) {
      wx.hideLoading()
      if (res.data.data.length === 0) return
      let list = [
        ...this.data.resList,
        ...res.data.data
      ].sort((a: ListHouseRes, b: ListHouseRes) => a.house_rent - b.house_rent)
      this.setData({
        resList: list
      })
    } else {
      wx.hideLoading()
      wx.showToast({ title: '无结果', icon: 'none' })
    }
  },
  toDetail(e: WechatMiniprogram.TouchEvent) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/houseDetail/houseDetail?id=${id}`
    })
  },
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    })
    this.addPage()
  }
})